import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QProgressBar, QPushButton
from PySide6.QtCore import QThread, Signal,QThreadPool
 
class WorkerSignals(QObject):
    finished = Signal()
    error = Signal(tuple)
    progress = Signal(int)
 
class Worker(QRunnable):
    signals = WorkerSignals()
    
    @pyqtSlot()
    def run(self):
        for i in range(101):  # 模拟一个长时间运行的任务
            self.signals.progress.emit(i)
            QThread.msleep(100)  # 模拟任务的暂停
        self.signals.finished.emit()
 
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.progress_bar = QProgressBar(self)
        self.progress_bar.setRange(0, 100)
        
        self.start_button = QPushButton('Start', self)
        self.start_button.clicked.connect(self.start_task)
 
        self.setCentralWidget(self.progress_bar)
        self.setWindowTitle('Progress Bar Example')
 
    def start_task(self):
        self.worker = Worker()
        self.worker.signals.progress.connect(self.update_progress)
        self.worker.signals.finished.connect(self.task_finished)
        
        QThreadPool.globalInstance().start(self.worker)
 
    def update_progress(self, n):
        self.progress_bar.setValue(n)
        if n == 100:
            self.progress_bar.setFormat('完成')
 
    def task_finished(self):
        self.start_button.setEnabled(True)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec())